/**
 *   类说明
 * 	@company         杭州黔灵科技有限公司  
 *   @creator             chenhe
 *   @create-time     2012-11-29   下午5:31:08   
 *   
 */
package com.zscg.sequence.ui;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zgcs.sequence.net.in.InPackage;
import com.zgcs.sequence.net.in.LoginPackage;
import com.zgcs.sequence.net.in.RegistPackage;
import com.zgcs.sequence.tool.ServiceTool;
import com.zscg.sequence.R;
import com.zscg.sequence.service.SequenceService;

public class LoginActiviity extends SequenceActivity implements OnClickListener {

	public static final int DIALOG_LOGIN = 1;
	public static final int DIALOG_REGIST = 2;

	EditText username, password;
	Button login, regist;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		login = (Button) findViewById(R.id.login);
		login.setOnClickListener(this);
		regist = (Button) findViewById(R.id.regist);
		regist.setOnClickListener(this);
		SharedPreferences userInfo = getSharedPreferences("user_info", 0);
		String userName = userInfo.getString("name", null);
		String passWord = userInfo.getString("pass", null);
		if (userName != null && passWord != null) {
			if (SequenceService.clinet != null) {
				showDialog(DIALOG_LOGIN);
				SequenceService.clinet.login(userName, passWord);
			} else {
				username.setText(userName);
				password.setText(passWord);
			}
		}

	}

	@Override
	protected void addselfFilter() {
		intentFilter.addAction(ServiceTool.ORDER_UR);
	}

	@Override
	protected void disposeSelf(Context context, Intent intent) {
		String action = intent.getAction();
		if (ServiceTool.ORDER_UR.equals(action)) {
			dismissDialog(DIALOG_REGIST);
			RegistPackage registPackage = (RegistPackage) intent
					.getSerializableExtra(ServiceTool.ORDER_UR);
			if (InPackage.RESULT_SUCESS == registPackage.getResult()) {
				showDialog(DIALOG_LOGIN);
				SequenceService.clinet.login(username.getText().toString(),
						password.getText().toString());
			} else {
				Toast.makeText(this, registPackage.getMsg(), Toast.LENGTH_LONG)
						.show();
			}
		}

	}

	@Override
	protected void disposeLogin(Intent intent) {
		dismissDialog(DIALOG_LOGIN);
		LoginPackage loginPackage = (LoginPackage) intent
				.getSerializableExtra(ServiceTool.ORDER_UL);

		if (loginPackage.getUser() != null) {

			SharedPreferences userInfo = getSharedPreferences("user_info", 0);
			userInfo.edit().putString("name", username.getText().toString())
					.commit();
			userInfo.edit().putString("pass", password.getText().toString())
					.commit();
			Intent mintent = new Intent(this, SequenceTabActivity.class);
			startActivity(mintent);
			finish();
		} else {
			Toast.makeText(this, loginPackage.getMsg(), Toast.LENGTH_LONG)
					.show();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login:
		case R.id.regist:
			String userName = username.getText().toString();
			String passWord = password.getText().toString();
			if (userName != null && userName.length() != 0) {
				if (passWord != null && passWord.length() != 0) {
					if (v.getId() == R.id.login) {
						showDialog(DIALOG_LOGIN);
						SequenceService.clinet.login(userName, passWord);
					} else {
						showDialog(DIALOG_REGIST);
						SequenceService.clinet.regist(userName, passWord);
					}
				} else
					Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(this, "用户名不能为空", Toast.LENGTH_LONG).show();
			}
			break;
		}

	}

	protected Dialog onCreateDialog(int id) {
		ProgressDialog dialog;
		String msg = null;
		switch (id) {
		case DIALOG_LOGIN:
			msg = "正在登录";
			break;
		case DIALOG_REGIST:
			msg = "正在注册";
			break;
		}
		dialog = new ProgressDialog(this);
		dialog.setIndeterminate(false);
		dialog.setMessage(msg);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setCancelable(true);
		dialog.show();
		return dialog;
	}

}
